package com.example.demo.service.impl;

import com.example.demo.client.SmQryClient;
import com.example.demo.mapper.NoticeMapper;
import com.example.demo.security.SecurityUtils;
import com.example.demo.service.NoticeService;
import com.example.demo.web.rest.constant.DatabaseConstant;
import com.example.demo.web.rest.constant.NoticeConstant;
import com.example.demo.web.rest.evt.NoticeByIdEvt;
import com.example.demo.web.rest.evt.NoticeSaveEvt;
import com.example.demo.web.rest.evt.NoticeSearchEvt;
import com.example.demo.web.rest.evt.TableIdSizeEvt;
import com.example.demo.web.rest.vm.NoticeCodeList;
import com.example.demo.web.rest.vm.NoticeVm;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {
    protected Logger logger= LoggerFactory.getLogger(getClass());

    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    protected SmQryClient baseQueryClient;

    @Override
    public NoticeCodeList getCodeList() {
        NoticeCodeList noticeCodeList = new NoticeCodeList();
        noticeCodeList.setTypeList(noticeMapper.getCode(NoticeConstant.TABLE_NAME,NoticeConstant.COL_NAME_TYPE));
        noticeCodeList.setStateList(noticeMapper.getCode(NoticeConstant.TABLE_NAME,NoticeConstant.COL_NAME_STATE));
        return noticeCodeList;
    }

    @Override
    public Integer selectInfosCount(NoticeSearchEvt evt) {
        return noticeMapper.selectInfosCount(evt);
    }

    @Override
    public List<NoticeVm> selectInfos(NoticeSearchEvt evt) {
        return noticeMapper.selectInfos(evt);
    }

    @Override
    public NoticeVm selectInfoById(NoticeByIdEvt evt) {
        return noticeMapper.selectInfoById(evt);
    }

    @Override
    public void saveInfos(NoticeSaveEvt evt) throws Exception{
        String username = SecurityUtils.getCurrentUserLogin().get();
//        String username =getCurrentUserAlias();
        String nowDate = nowDate();
        NoticeVm vm = new NoticeVm();
        BeanUtils.copyProperties(evt,vm);
        if(StringUtils.isBlank(evt.getNoticeId())){
            // id为空，新增
            TableIdSizeEvt tableIdSizeEvt = new TableIdSizeEvt(DatabaseConstant.TABLE_NAME_NOTICE,DatabaseConstant.ID_SIZE);
            vm.setNoticeId(baseQueryClient.getTableId(tableIdSizeEvt));
            vm.setCreateTime(nowDate);
            vm.setCreateUser(username);
            vm.setUpdateTime(nowDate);
            vm.setUpdateUser(username);
            // 默认草稿
            vm.setState(NoticeConstant.DRAFT);
            noticeMapper.insertNotice(vm);
        } else {
            vm.setNoticeId(Long.parseLong(evt.getNoticeId()));
            vm.setUpdateTime(nowDate);
            vm.setUpdateUser(username);
            // 编辑
            noticeMapper.editNotice(vm);
        }
    }

    @Override
    public void publishNotice(NoticeByIdEvt evt) throws Exception {
        String username = SecurityUtils.getCurrentUserLogin().get();
//        String username =getCurrentUserAlias();
        String nowDate = nowDate();
        NoticeVm vm = new NoticeVm();
        BeanUtils.copyProperties(evt,vm);
        vm.setState(NoticeConstant.PUBLISH);
        vm.setPublishTime(nowDate);
        vm.setPublishUser(username);
        noticeMapper.publishNotice(vm);
    }

    @Override
    public void deleteInfoById(NoticeByIdEvt evt) {
        noticeMapper.deleteById(evt);
    }

    public String nowDate(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(new Date());
    }

    @Override
    public List<NoticeVm> getLatestNotice(int limitNum)
    {
        return noticeMapper.getLatestNotice(limitNum);
    }

}
